package com.xb.report.controller;


import com.github.pagehelper.Page;
import com.xb.report.service.IDataCountService;
import com.xb.report.util.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author gxd
 * jar运行 注意new modelAndView 开头没有/
 */

@Controller
@RequestMapping(value = "da")
public class AnalysisController {

    @Autowired
    IDataCountService dataCountService;

    Logger logger=LoggerFactory.getLogger(this.getClass());



    /**
     *
     * startDate--endDate
     * @return
     */
    @RequestMapping(value = "/getBrowserData")
    public @ResponseBody
    Map<String, Object> getBrowserData(HttpServletRequest request, HttpServletResponse res) {
        res.setHeader("Access-Control-Allow-Origin", "*");
        ModelMap map = new ModelMap();
        Integer r = null;
        String date=request.getParameter("date");

        String startDate="";
        String endDate="";

        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
        }else{//默认30天
            endDate=getEndDate();
            startDate=getStartDate();
        }
        //wechat chrome qqbrowser
        List<Map<String, Object>> wechatList= dataCountService.getDaBrowser(startDate,endDate,"Wechat");
        List<Map<String, Object>> chromeList= dataCountService.getDaBrowser(startDate,endDate,"Chrome");
        List<Map<String, Object>> qqbrowserList= dataCountService.getDaBrowser(startDate,endDate,"QQbrowser");

        map.put("wechatList",wechatList);
        map.put("chromeList",chromeList);
        map.put("qqList",qqbrowserList);

        return map;
    }



    /**
     * 获取YYYY-MM-DD格式 前一天
     *
     * @return
     */
    private String getEndDate() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date today=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        today = calendar.getTime();
        return sdf.format(today);
    }
    private  String getStartDate() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date today=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);
        calendar.add(Calendar.DAY_OF_MONTH, -30);
        today = calendar.getTime();
        return sdf.format(today);
    }




}
